package com.example.ydkf1;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class Danmu extends AppCompatActivity {
    private boolean ShowDanmaku;
    private DanmakuView danmakuView;
    private DanmakuContext danmakuContext;
    private Button sendButton;
    private LinearLayout sendLayout;
    private EditText editText;
    private VideoView videoView;


    private boolean showDanmaku;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.danm);
        initView();
        playView();
        initDanmaku();
    }
    private void initView(){
        videoView = (VideoView)findViewById(R.id.videoview);
        sendLayout = (LinearLayout) findViewById(R.id.ly_send);
        sendButton = (Button) findViewById(R.id.send);
        editText = (EditText)findViewById(R.id.et_text);
        danmakuView = (DanmakuView)findViewById(R.id.danmaku);
    }
    private void playView(){
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.sp;
        if (uri != null){
            videoView.setVideoURI(Uri.parse(uri));
            videoView.start();
        }else{
            videoView.getBackground().setAlpha(0);
        }
    }
    private BaseDanmakuParser parser = new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };

    private void  initDanmaku(){
        danmakuView.setCallback(new DrawHandler.Callback(){
            @Override
            public  void prepared(){
                showDanmaku = true;
                danmakuView.start();
                generateDanmakus();
            }
            @Override
            public void  updateTimer(DanmakuTimer timer){}
            @Override
            public void danmakuShown(BaseDanmaku danmaku){}
            @Override
            public void drawingFinished() {
            }
        });
        danmakuContext = DanmakuContext.create();
        danmakuView.enableDanmakuDrawingCache(true);
        danmakuView.prepare(parser,danmakuContext);
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(sendLayout.getVisibility()==View.GONE) {
                    sendLayout.setVisibility(View.VISIBLE);
                }else {
                    sendLayout.setVisibility(View.GONE);
                }
            }
        });
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=editText.getText().toString();
                if(!TextUtils.isEmpty(content)){
                    addDanmaku(content,true);
                    editText.setText("");
                }
            }
        });
    }

    /***
     * 随机产生一些弹幕
     */
    private void generateDanmakus() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (showDanmaku) {
                    int time = new Random().nextInt(300);
                    String content = "这是一条弹幕" + time;
                    addDanmaku(content, false);
                    try {
                        Thread.sleep(time);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    /***
     * 添加弹幕的方法
     * @param content 弹幕的内容
     * @param border 是否是用户发送的弹幕
     */
    private void addDanmaku(String content,boolean border) {
        BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text = content;
        danmaku.padding = 6;
        danmaku.textSize = 48;
        danmaku.setTime(danmakuView.getCurrentTime());
        danmaku.textColor = Color.WHITE;
        if (border){
            danmaku.borderColor = Color.GREEN;
        }
        danmakuView.addDanmaku(danmaku);
    }

}


